package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.PayInfoEntity;
import com.sgb.kjwl.model.entity.transport.order.MyOrderEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: 黄淋
 * @date: 2020/2/5 18:14
 * @Description:
 */
public class TransportOrderDriverViewModel extends AndroidViewModel {
    private int pageNum = 1;
    public MutableLiveData<List<MyOrderEntity>> myOrderEntity = new MutableLiveData<>();
    public MutableLiveData<PayInfoEntity> ldTradeNo = new MutableLiveData<>();

    public TransportOrderDriverViewModel(@NonNull @NotNull Application application) {
        super(application);
    }


    /**
     * 获取订单列表
     */
    public void getList(Context context, boolean isLodMore,  Map  map) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        map.put("limit", 10);
        map.put("page", pageNum);
        DialogHelper.showProgressDialog(context, "请求中...");
        NewAPIManager.getInstance().getLogisticsAPI().getGoodsDriverOrder(map).enqueue(new Callback<BaseEntity<ListResponse<List<MyOrderEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<MyOrderEntity>>>> call, Response<BaseEntity<ListResponse<List<MyOrderEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null) {
                    return;
                }
                myOrderEntity.postValue(response.body().getData().getList());
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<MyOrderEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 支付运费
     *
     * @param orderNo
     */
    public void getPayFreight(Context context, String orderNo) {
        Map map = new HashMap();
        map.put("orderNo", orderNo);
        DialogHelper.showProgressDialog(context, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().payFreight(map).enqueue(new Callback<BaseEntity<PayInfoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<PayInfoEntity>> call, Response<BaseEntity<PayInfoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getData() == null)
                    return;

                if (response.body().getState().equals("ok") && response.body().getData().getResult().equals("200")) {
                    ldTradeNo.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getData().getMessage());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<PayInfoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
